package com.itheima.app.controller;

import com.itheima.app.manager.MovementManager;
import com.itheima.domain.mongo.Movement;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {

    @Autowired
    MovementManager movementManager;

    //发布动态
    @PostMapping("/movements")
    public void save(MultipartFile[] imageContent, Movement movement) throws IOException {
        movementManager.save(imageContent,movement);
    }

    //分页查询个人动态列表
    @GetMapping("/movements/all")
    public ResponseEntity findMyMovementByPage(
            Long userId,
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){

        return movementManager.findMyMovementByPage(userId,pageNum,pageSize);
    }

    //分页查询好友动态列表
    @GetMapping("/movements")
    public ResponseEntity findFriendMovementByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){

        return movementManager.findFriendMovementByPage(pageNum,pageSize);
    }

    //分页查询推荐动态列表
    @GetMapping("/movements/recommend")
    public ResponseEntity findRecommendMovementByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){

        return movementManager.findRecommendMovementByPage(pageNum,pageSize);
    }

    //对动态进行点赞
    @GetMapping("/movements/{publishId}/like")
    public ResponseEntity movementLike(@PathVariable ObjectId publishId){
        return movementManager.movementLike(publishId);
    }

    //对动态取消点赞
    @GetMapping("/movements/{publishId}/dislike")
    public ResponseEntity movementDisLike(@PathVariable ObjectId publishId){
        return movementManager.movementDisLike(publishId);
    }

    //对动态进行喜欢
    @GetMapping("/movements/{publishId}/love")
    public ResponseEntity movementLove(@PathVariable ObjectId publishId){
        return movementManager.movementLove(publishId);
    }

    //对动态取消喜欢
    @GetMapping("/movements/{publishId}/unlove")
    public ResponseEntity movementUnLove(@PathVariable ObjectId publishId){
        return movementManager.movementUnLove(publishId);
    }

    //查询单条动态
    @GetMapping("/movements/{movementId}")
    public ResponseEntity findById(@PathVariable ObjectId movementId){
        return movementManager.findById(movementId);
    }

    //分页查询评论列表
    @GetMapping("/comments")
    public ResponseEntity findCommentByPage(
            String movementId,
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        return movementManager.findCommentByPage(new ObjectId(movementId),pageNum,pageSize);
        //return null;
    }

    //发布评论
    @PostMapping("/comments")
    public ResponseEntity movementComment(@RequestBody Map<String,String> map){
        return movementManager.movementComment(map);
    }
}
